/*	BuildDepot class definition.
 *
*/

#include "BuildingPlacer.h"
#include "BuildDepot.h"

void BuildDepot::Enter()
{
	DEBUG_PRINT("BuildDepot(%p) %s entering\n", this, GetName().c_str());

}

void BuildDepot::Execute()
{
	DEBUG_PRINT("BuildDepot(%p) %s executing\n", this, GetName().c_str());
	if(!builder)
	{
		std::list<BWAPI::Unit*>* scvs = (base)->GetUnitsByType(BWAPI::UnitTypes::Terran_SCV);
		std::list<BWAPI::Unit*>::iterator it = scvs->begin();
		if (!builder)
		{
			for (; it!=scvs->end(); it++)
			{
				if (((*it)->isGatheringMinerals() || (*it)->isIdle()) && !(*it)->isCarryingMinerals()) break;
			}
			if(it!=scvs->end())
				builder = (*it);
			else
			{
				control->FinishWithSuccess();
				return;
			}
		}
		
		BWAPI::TilePosition pos = BuildPlacer->getBuildLocationNear(base->GetBaseLocation(),BWAPI::UnitTypes::Terran_Supply_Depot);
		DEBUG_PRINT("BuildDepot(%p) attempting to build depot at tile position(%d,%d).\n", this, pos.x(),pos.y());
		
		builder->build(pos,BWAPI::UnitTypes::Terran_Supply_Depot);
		BB->ResetOrder();
	}
	else if(builder)
	{
		if(!builder->exists())
		{
			builder = NULL;
			BB->Refund(BWAPI::UnitTypes::Terran_Supply_Depot);
		}
		else if (builder->isConstructing()&& builder->getBuildUnit())
			builder = NULL;
		else if( builder->getBuildType() == BWAPI::UnitTypes::Terran_Supply_Depot)
		{

		}
		else
		{
			//BB->Refund(BWAPI::UnitTypes::Terran_Supply_Depot);
			builder = NULL;
		}
	}

	control->FinishWithSuccess();
}

void BuildDepot::Exit()
{
	DEBUG_PRINT("BuildDepot(%p) %s exiting\n", this, GetName().c_str());
	base = NULL;

}

bool BuildDepot::CheckConditions()
{
	DEBUG_PRINT("BuildDepot(%p) %s checking conditions\n", this, GetName().c_str());
	std::list<Base*>* bases = BB->GetBases();
	std::list<BWAPI::Unit*>* depot = BB->GetBuildingsByTypeGeneral(BWAPI::UnitTypes::Terran_Supply_Depot);
	int depotCount = INT_MAX;
	int simultaneousDepots = (BWAPI::Broodwar->self()->supplyTotal()/2 < 12) ? 1 : 2;
	if(BWAPI::Broodwar->self()->incompleteUnitCount(BWAPI::UnitTypes::Terran_Supply_Depot) < simultaneousDepots)
	{
		if((BWAPI::Broodwar->self()->supplyTotal()-BWAPI::Broodwar->self()->supplyUsed())/2 <=2)
		{
			if(bases)
			{
				/*for(std::list<Base*>::iterator b = bases->begin(); b != bases->end(); b++)
				{
					if(depot = (*b)->GetBuildingsByType(BWAPI::UnitTypes::Terran_Supply_Depot))
					{
						if(depot->size() < depotCount)
						{
							depotCount = depot->size();
							base = *b;
							break;
						}
					}
					else
					{
						base = *b;
						break;
					}
				}*/
				base = bases->back();
				if(base)
				{
					if(BB->isBuildBuilding(BWAPI::UnitTypes::Terran_Supply_Depot))
					{
						
						DEBUG_PRINT("BuildDepot(%p) %s checked conditions and has %d minerals to spend\n", this, GetName().c_str(), BB->GetMineralsToSpend());
						BB->SpendResources(BWAPI::UnitTypes::Terran_Supply_Depot);
						return true;
					}
					else if(builder)
						return true;
				}
			}
		}
	}
	return false;
}